uniform sampler2D suggestiveContours, depthTexture;
uniform vec2 screenSize;
uniform vec4 color;         


#define s2(a, b)				temp = a; a = min(a, b); b = max(temp, b);
#define t2(a, b)				s2(pixels[a], pixels[b]);
#define t24(a, b, c, d, e, f, g, h)		t2(a, b); t2(c, d); t2(e, f); t2(g, h); 
#define t25(a, b, c, d, e, f, g, h, i, j)	t24(a, b, c, d, e, f, g, h); t2(i, j);

  
void main(void) { 
 
  float depth;
  float minDepth= 1.0;
  float centerDepth= texture2D(depthTexture, gl_FragCoord.xy/screenSize).x;
  
  float temp;

  int i;

  float pixels[25];

  vec2 offsets[25];
  
  offsets[0]=   vec2(-2.0, -2.0);
  offsets[1]=   vec2(-2.0, -1.0);
  offsets[2]=   vec2(-2.0,  0.0);
  offsets[3]=   vec2(-2.0,  1.0);
  offsets[4]=   vec2(-2.0,  2.0);
  offsets[5]=   vec2(-1.0, -2.0);
  offsets[6]=   vec2(-1.0, -1.0);
  offsets[7]=   vec2(-1.0,  0.0);
  offsets[8]=   vec2(-1.0,  1.0);
  offsets[9]=   vec2(-1.0,  2.0);	
  offsets[10]=  vec2( 0.0, -2.0);
  offsets[11]=  vec2( 0.0, -1.0);
  offsets[12]=  vec2( 0.0,  0.0);
  offsets[13]=  vec2( 0.0,  1.0);
  offsets[14]=  vec2( 0.0,  2.0);
  offsets[15]=  vec2( 1.0, -2.0);
  offsets[16]=  vec2( 1.0, -1.0);
  offsets[17]=  vec2( 1.0,  0.0);
  offsets[18]=  vec2( 1.0,  1.0);
  offsets[19]=  vec2( 1.0,  2.0);
  offsets[20]=  vec2( 2.0, -2.0);
  offsets[21]=  vec2( 2.0, -1.0);
  offsets[22]=  vec2( 2.0,  0.0);
  offsets[23]=  vec2( 2.0,  1.0);
  offsets[24]=  vec2( 2.0,  2.0);

  vec2 offset;

  for(i=0; i<12; i++){
    pixels[i] = texture2D(suggestiveContours, (gl_FragCoord.xy+offsets[i])/screenSize).r;
    if(centerDepth==1.0){
      depth= texture2D(depthTexture, (gl_FragCoord.xy+offsets[i])/screenSize).x;
      if(depth<minDepth) minDepth= depth;
    }
  }
  for(i=12; i<25; i++){
    pixels[i] = texture2D(suggestiveContours, (gl_FragCoord.xy+offsets[i])/screenSize).r; 
    if(centerDepth==1.0){
      depth= texture2D(depthTexture, (gl_FragCoord.xy+offsets[i])/screenSize).x;
      if(depth<minDepth) minDepth= depth;
    }
  }    

  t25(0, 1,			3, 4,		2, 4,		2, 3,		6, 7);
  t25(5, 7,			5, 6,		9, 7,		1, 7,		1, 4);
  t25(12, 13,		11, 13,		11, 12,		15, 16,		14, 16);
  t25(14, 15,		18, 19,		17, 19,		17, 18,		21, 22);
  t25(20, 22,		20, 21,		23, 24,		2, 5,		3, 6);
  t25(0, 6,			0, 3,		4, 7,		1, 7,		1, 4);
  t25(11, 14,		8, 14,		8, 11,		12, 15,		9, 15);
  t25(9, 12,		13, 16,		10, 16,		10, 13,		20, 23);
  t25(17, 23,		17, 20,		21, 24,		18, 24,		18, 21);
  t25(19, 22,		8, 17,		9, 18,		0, 18,		0, 9);
  t25(10, 19,		1, 19,		1, 10,		11, 20,		2, 20);
  t25(2, 11,		12, 21,		3, 21,		3, 12,		13, 22);
  t25(4, 22,		4, 13,		14, 23,		5, 23,		5, 14);
  t25(15, 24,		6, 24,		6, 15,		7, 16,		7, 19);
  t25(3, 11,		5, 17,		11, 17,		9, 17,		4, 10);
  t25(6, 12,		7, 14,		4, 6,		4, 7,		12, 14);
  t25(10, 14,		6, 7,		10, 12,		6, 10,		6, 17);
  t25(12, 17,		7, 17,		7, 10,		12, 18,		7, 12);
  t24(10, 18,		12, 20,		10, 20,		10, 12);
	  
  
  if(pixels[12]!=0.0){
    gl_FragColor= vec4(color.rgb, pixels[12]);
    if(centerDepth==1.0) gl_FragDepth= minDepth;
    else gl_FragDepth= gl_FragCoord.z;	
  }  
  else{
    discard;
  }

  //gl_FragColor= texture2D(suggestiveContours, (gl_FragCoord.xy)/screenSize);
}